package presentation;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import businessObject.PuntuacionMascota;

/**
 * Servlet implementation class GestionVotar
 */
public class GestionVotar extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GestionVotar() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		
		Integer idUsuario = (Integer)session.getAttribute("session.idusuario");
		String mascota=request.getParameter("idMascota");
		String puntuacion=request.getParameter("puntuacion");
		String resource=request.getParameter("resource");
		if(idUsuario !=null){
			if(mascota !=null && puntuacion !=null){
				insertarPuntuacion(idUsuario, mascota, puntuacion);
				response.sendRedirect(resource);
			}
			else{
				response.sendRedirect(resource+"?mascota=no");
			}
		}
		else{
			response.sendRedirect(resource);
		}
		
	}
	
	private void insertarPuntuacion(Integer IdUsuario, String mascota,String puntuacion){
		
		Integer idMascota=Integer.parseInt(mascota);
		Integer punt=Integer.parseInt(puntuacion);
		PuntuacionMascota puntuacion2=PuntuacionMascota.getInstance();
		puntuacion2.votarMascota(idMascota, IdUsuario, punt);

	}

}
